<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MAdmin;
use App\Models\MAdminOperaLog;
use App\Models\MConfigs;
use App\Models\MRoles;
use Illuminate\Http\Request;

/**
 * 设置控制器
 */
class SettingController extends Controller
{

    /** 保存网站设置
     * @param Request $request
     */
    public function webSet(Request $request)
    {
        if (!$request->has(['name','title'])) {
            $this->data = $this->makeJsCode(1,'请填写完整数据');
            return $this->apiJson();
        }

        $config = MConfigs::where('title', $request->title)->first();
        if (!$config) {
            $config = new MConfigs();
        }

        if ($config->saveConfig($request)) {
            MAdminOperaLog::log(session("admin_id"), $request->getClientIp(), '编辑网站设置', "管理员[id:".session('admin_id').",name:".session('admin_name')."]编辑网站");
            $this->data = $this->makeJsCode(0, '保存成功');
        } else {
            $this->data = $this->makeJsCode(1, '保存失败');
        }

        return $this->apiJson();
    }

    /**
     * 获取配置信息
     */
    public function configPage(Request $request)
    {
        if (!$request->has('title')) {
            $this->data = $this->makeJsCode(1, '请求参数不正确');
            return $this->apiJson();
        }
        $config = MConfigs::where('title',$request->title)->first();
        if ($config) {
            $kv = unserialize($config->kv);

            $data['webName'] = $kv['webName'] ?? '';
            $data['logoPath'] = $kv['logoPath'] ? asset($kv['logoPath']) : '';
            $data['caseNumber'] = $kv['caseNumber'] ?? '';
            $data['linkman'] = $kv['linkman'] ?? '';
            $data['tel'] = $kv['tel'] ?? '';
            $data['address'] = $kv['address'] ?? '';
            $data['openDown'] = $kv['openDown'] ?? '';
            $data['downDesc'] = $kv['downDesc'] ?? '';
        }
        $this->data = $this->makeJsCode(0,'获取成功！');
        $this->data['info'] = $data;

        return $this->apiJson();
    }

    /**
     * 后台管理员列表
     */
    public function adminsList()
    {
        $admins = MAdmin::all();
        $info = array();
        if (!$admins->isEmpty()) {
            foreach ($admins as $admin) {
                if ($admin->role->super != 1) {
                    $item['id'] = $admin->id;
                    $item['name'] = $admin->name;
                    $item['roleName'] = $admin->role->name;
                    $info[] = $item;
                }
            }
        }
        $this->data = $this->makeJsCode(0, '获取成功！');
        $this->data['info'] = $info;
        return $this->apiJson();
    }

    /**
     * 管理员编辑界面
     */
    public function adminsEdit(Request $request)
    {
        $admin = MAdmin::find($request->id);
        if (!$admin) {
            $this->data = $this->makeJsCode(1,'管理员不存在！');
            return $this->apiJson();
        }

        $this->data = $this->makeJsCode(0,'获取成功');
        $info['id'] = $admin->id;
        $info['name'] = $admin->name;
        $info['role_id'] = $admin->role_id;
        $this->data['info'] = $info;

        return $this->apiJson();
    }

    /**
     * 保存管理员
     */
    public function adminsSave(Request $request)
    {
        if (!$this->validateReq($request, MAdmin::saveRulesWithMsg(0), MAdmin::saveRulesWithMsg(1))) {
            $this->data = $this->makeJsCode(1, $this->errors->first());
            return $this->apiJson();
        }

        $admin = MAdmin::find($request->id);
        if (!$admin) {
            if (MAdmin::checkName($request,true)) {
                $this->data = $this->makeJsCode(1, '用户名已存在！');
                return $this->apiJson();
            }
            $admin = new MAdmin();
        } else {
            if (MAdmin::checkName($request,false)) {
                $this->data = $this->makeJsCode(1,'用户名已存在！');
                return $this->apiJson();
            }
        }

        if ($admin->saveAdmin($request)) {
            MAdminOperaLog::log(session("admin_id"), $request->getClientIp(), '编辑管理员', "管理员[id:".session('admin_id').",name:".session('admin_name')."]编辑管理员：{$admin->name}");
            $this->data = $this->makeJsCode(0, '保存成功！');
        } else {
            $this->data = $this->makeJsCode(1, '保存失败');
        }

        return $this->apiJson();
    }

    /**
     * 删除管理员
     */
    public function adminsDel(Request $request)
    {
        $admin = MAdmin::find($request->id);
        if (!$admin) {
            $this->data = $this->makeJsCode(1, '管理员不存在');
            return $this->apiJson();
        }

        if ($admin->delete()) {
            MAdminOperaLog::log(session("admin_id"), $request->getClientIp(), '删除管理员', "管理员[id:".session('admin_id').",name:".session('admin_name')."]删除管理员：{$admin->name}");
           $this->data = $this->makeJsCode(0, '删除成功');
        } else {
            $this->data = $this->makeJsCode(1, '删除失败');
        }

        return $this->apiJson();
    }

    /**
     * 角色列表
     */
    public function rolesList()
    {
       $roles = MRoles::all();
       $info = [];
       if (!$roles->isEmpty()) {
           foreach ($roles as $role) {
               if ($role->super != 1) {
                   $item['id'] = $role->id;
                   $item['name'] = $role->name;
                   $info[] = $item;
               }
           }
       }
       $this->data = $this->makeJsCode(0, '获取成功！');
       $this->data['info'] = $info;

       return $this->apiJson();
    }
}